package com.sky.controller.admin;

import com.sky.properties.AliyunOSSProperties;
import com.sky.result.Result;
import com.sky.utils.AliyunOSSUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;

@RestController
@RequestMapping("/admin/common")
@Slf4j
public class UploadController {
    @Autowired
    private AliyunOSSProperties aliyunOSSProperties;

    @PostMapping("/upload")
    public Result upload(MultipartFile file) throws Exception {
        byte[] bytes = file.getBytes();
        String originalFilename = file.getOriginalFilename();
        int i = originalFilename.lastIndexOf('.');
        String substring = originalFilename.substring(i);
        String url = AliyunOSSUtils.upload(aliyunOSSProperties.getEndpoint(), aliyunOSSProperties.getBucketName(), bytes, substring);
        return Result.success(url);
    }
}
